import TopNav from "@/components/TopNav/index.vue"
import MainNav from "@/components/MainNav/index.vue"
import {
    getProjectTypes,
    getmenu
} from "../../../api/index"
import {getToken} from '@u/token.js'

export default {
    components: {
        TopNav,
        MainNav
    },
    data() {
        return {
            activeName: '',
            src: require("../../../assets/test02.jpg"),
            tabList: [],
            menuList: [],
            activeList: [],
            loading: null,
            loadingCount: 1
        }
    },
    created() {
        // let href = window.location.href
        // let token = href.substring(href.indexOf('=')+1,href.lastIndexOf('#'))
        let token = this.$route.query.ACCESS_TOKEN
        if (token) {
            let _this = this
            this.$store
                .dispatch("user/saveToken", token)
                .then(() => {
                    _this.$store
                        .dispatch("user/getUserInfo", token)
                        .then(response => {
                            // console.log(response)
                        })
                })
        } else {
            console.log(this.$route.query)
        }


        this.handleHttp()
    },
    methods: {
        handleClick(tab, event) {
            if (this.menuList && this.menuList.length > 0) {
                this.activeList = this.menuList.filter(item => item.type == this.activeName)
            } else {
                this.activeList = []
            }

        },
        // 获取 Tab 栏
        handleGetTabs() {
            getProjectTypes()
                .then(res => {
                    if (res.data.code === 200) {
                        this.handleGetMenu()
                        this.tabList = res.data.result
                        this.activeName = res.data.result[0].id + ''

                    } else {
                        this.loading.close()
                        this.$message({
                            message: res.data.msg+'tab',
                            type: 'warning'
                        })
                    }

                    if (this.loadingCount === 2) {
                        this.loading.close()
                        this.loadingCount = 1
                    } else {
                        ++this.loadingCount
                    }
                })
                .catch(err => {
                    this.loading.close()
                    // this.$message.error("获取tab失败")
                })
        },

        // 获取 列表
        handleGetMenu() {
            let token = getToken()
            getmenu({
                loginUser: token ? true : false
            })
                .then(res => {
                    if (res.data.code === 200) {
                        this.menuList = res.data.result
                        this.activeList = this.menuList.filter(item => item.type === this.tabList[0].id)
                    } else {
                        this.loading.close()
                        this.$message({
                            message: res.data.msg+'列表',
                            type: 'warning'
                        })
                    }
                    if (this.loadingCount == 2) {
                        this.loading.close()
                        this.loadingCount = 1
                    } else {
                        ++this.loadingCount
                    }
                })
                .catch(err => {
                    this.loading.close()
                    // this.$message.error("获取列表失败")
                })
        },

        // 统一发送请求
        handleHttp() {
            this.loading = this.$loading({
                lock: true,
                text: '获取数据中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            })
            this.handleGetTabs()
        },

        // 进入系统
        goSystem(url) {
            let token = getToken()
            if (token) {
                window.open(url, '_blank')
            } else {
                this.$confirm('您还没有登录，请先登录一下吧', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'info'
                }).then(() => {
                    window.location.href = process.env.VUE_APP_LOGIN_URL
                    localStorage.setItem("isRoute", "false")
                }).catch(() => {
                    // 点击取消
                })
            }
        }
    }
}
